package peer.assist.message;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;

public class ReverseConnTCPMessage1 extends Message{
	public short sType=peer.tools.MessageType.sType_ReverseConnTCPMessage1;
	byte[] dpData;
	int assistSessionId;
	int revConnTCPSessionId;
	InetAddress dstIp;
	int dstPort;
	public ReverseConnTCPMessage1(int revConnTCPSessionId,int assistSessionId,InetAddress dstIp,int dstPort){
		dpData=new byte[20];
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		ByteIntSwitch.toByteArray(assistSessionId, dpData, 4);
		ByteIntSwitch.toByteArray(revConnTCPSessionId, dpData, 8);
		System.arraycopy(dstIp.getAddress(), 0, dpData, 12, 4);
		ByteIntSwitch.toByteArray(dstPort, dpData, 16);
		dp=new DatagramPacket(dpData,dpData.length);
	}
	public ReverseConnTCPMessage1(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		assistSessionId=ByteIntSwitch.toInt(dpData, 4);
		revConnTCPSessionId=ByteIntSwitch.toInt(dpData, 8);
		byte[] b=new byte[4];
		System.arraycopy(dpData, 12, b, 0, 4);
		try {
			dstIp=InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		dstPort=ByteIntSwitch.toInt(dpData, 16);
		
	}
	
	public InetAddress getDstAddress(){
		return dstIp;
	}
	
	public int getDstPort(){
		return dstPort;
	}

	public int getRevConnTCPSessionId(){
		return revConnTCPSessionId;
	}
	
	public int getAssistSessionId(){
		return assistSessionId;
	}

}
